//
// Created by fazhehy on 2024/6/7.
//

#include "key.h"

uint8_t get_key_num()
{
    bool isLongPressure = false;
    uint32_t tick = 0;
    uint8_t keyNum = 0;

    if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3) == GPIO_PIN_RESET){
        HAL_Delay(20);
        tick = HAL_GetTick();
        while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3) == GPIO_PIN_RESET){
            if (tick <= HAL_GetTick() - 500){
                isLongPressure = true;
            }
        }
        HAL_Delay(20);
        if (isLongPressure)
            keyNum = 11;
        else
            keyNum = 1;
    }
    else if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4) == GPIO_PIN_RESET){
        HAL_Delay(20);
        tick = HAL_GetTick();
        while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4) == GPIO_PIN_RESET){
            if (tick <= HAL_GetTick() - 500){
                isLongPressure = true;
            }
        }
        HAL_Delay(20);
        if (isLongPressure)
            keyNum = 22;
        else
            keyNum = 2;
    }
    else if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10) == GPIO_PIN_RESET){
        HAL_Delay(20);
        tick = HAL_GetTick();
        while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10) == GPIO_PIN_RESET){
            if (tick <= HAL_GetTick() - 500){
                isLongPressure = true;
            }
        }
        HAL_Delay(20);
        if (isLongPressure)
            keyNum = 33;
        else
            keyNum = 3;
    }
    else if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == GPIO_PIN_RESET){
        HAL_Delay(20);
        tick = HAL_GetTick();
        while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == GPIO_PIN_RESET){
            if (tick <= HAL_GetTick() - 500){
                isLongPressure = true;
            }
        }
        HAL_Delay(20);
        if (isLongPressure)
            keyNum = 44;
        else
            keyNum = 4;
    }
    else if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == GPIO_PIN_RESET){
        HAL_Delay(20);
        tick = HAL_GetTick();
        while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == GPIO_PIN_RESET){
            if (tick <= HAL_GetTick() - 500){
                isLongPressure = true;
            }
        }
        HAL_Delay(20);
        if (isLongPressure)
            keyNum = 55;
        else
            keyNum = 5;
    }
    else if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6) == GPIO_PIN_RESET){
        HAL_Delay(20);
        tick = HAL_GetTick();
        while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6) == GPIO_PIN_RESET){
            if (tick <= HAL_GetTick() - 500){
                isLongPressure = true;
            }
        }
        HAL_Delay(20);
        if (isLongPressure)
            keyNum = 66;
        else
            keyNum = 6;
    }
    return keyNum;
}
